#include "Character.h"
#include "FastMath.h"

void Character::Render(){
	//if (mouse_state.x<render_pos_.m_Position[0])
	//	m_sVelocity.x=-10;
	//else 
	//	m_sVelocity.x=10;
	//if (FastMath::Abs(mouse_state.x-render_pos_.m_Position[0])<10)
	//{
	//	m_sVelocity.x=0;
	//}
	//} 
	
	if (g_Timer.GetTime_MS()>TIMESLOT)
	{
		///Position Calculation
		m_sVelocity.x=(mouse_state.x-render_pos_.m_Position[0]-stride_x/2)*0.2;
		if (m_sVelocity.x>200)m_sVelocity.x=200;
		else if (m_sVelocity.x<-200)m_sVelocity.x=-200;
		global_pos_.x=render_pos_.m_Position[0]+=m_sVelocity.x;
		g_Timer.Restart();
	
		m_sVelocity.y+=1;
		//render_pos_.m_Position[1]=mouse_state.y;
		//printf("Char:%d,%d,%d,%d\n",render_pos_.m_Position[0],render_pos_.m_Position[1],mouse_state.x-render_pos_.m_Position[0],m_sVelocity.x);
		
		///Animation calculation
		///Texture Coordinate
		if (m_sVelocity.x>0)
			m_eState=NORMAL_RIGHT;
		if (m_sVelocity.x<0)
			m_eState=NORMAL_LEFT;
		if ((m_sVelocity.y)<-20)
			m_eState=UP_SWIFT;

		render_pos_.m_Texcoord[1]=stride_y*((int)m_eState);
		switch(m_eState){
		case NORMAL_LEFT:
		case NORMAL_RIGHT:
			if (m_sVelocity.y<-10) render_pos_.m_Texcoord[0]=0;
			else if (m_sVelocity.y<-7) render_pos_.m_Texcoord[0]=stride_x;
			else if (m_sVelocity.y<-4) render_pos_.m_Texcoord[0]=stride_x*2;
			else if (m_sVelocity.y<-2) render_pos_.m_Texcoord[0]=stride_x*3;
			else if (m_sVelocity.y<=0) render_pos_.m_Texcoord[0]=stride_x*4;
			else if (m_sVelocity.y<2) render_pos_.m_Texcoord[0]=stride_x*5;
			else if (m_sVelocity.y<3) render_pos_.m_Texcoord[0]=stride_x*6;
			else if (m_sVelocity.y<4) render_pos_.m_Texcoord[0]=stride_x*7;
			else if (m_sVelocity.y<7) render_pos_.m_Texcoord[0]=stride_x*8;
			else render_pos_.m_Texcoord[0]=stride_x*9;
	//		printf("%d,%d\n",render_pos_.m_Texcoord[0],m_sVelocity.y);
			break;
		case UP_SWIFT:
			render_pos_.m_Texcoord[0]=stride_x*(current_ani_-1);
			current_ani_=(current_ani_++)%ani_count_[(int)m_eState];
			break;
		}
		//render_pos_.m_Texcoord[0]=stride_x*(current_ani_-1);
		//render_pos_.m_Texcoord[1]=stride_y*((int)m_eState);
		//current_ani_=(current_ani_++)%ani_count_[(int)m_eState];
	}


	///Physical calculation
	m_iPoint.x=render_pos_.m_Position[0]+stride_x/2;
	m_iPoint.y=render_pos_.m_Position[1]+stride_y/2;
	
	RenderFrameDX9(this,Resource::instance().m_Character);


}

void Character::GetImageFromFile(const char* _filename)
{
	file_name_=_filename;
}

void Character::SetAnimation(int _type)
{
	m_eState=(State)_type;
}

Character::Character()
{

	m_fRadius=64;
	m_sVelocity.x=0;
	m_sVelocity.y=0;
	touch_type_=ITouchable::T_CIRCLE;
	render_pos_.m_Texcoord[0]=0;
	render_pos_.m_Texcoord[1]=0;
	current_ani_=1;
	m_eState=Character::NORMAL_LEFT;
	ani_count_[UP_SWIFT]=8;
}

Character::~Character()
{

}

bool Character::Touch(ITouchable& _touch)
{
	switch (_touch.touch_type_)
	{
	case T_CIRCLE:
		printf("Colliding:%d,%d,%d,%d\n",this->m_iPoint.x,_touch.m_iPoint.x,this->m_iPoint.y,_touch.m_iPoint.y);
		if(FastMath::Sqrt(sqr(this->m_iPoint.x-_touch.m_iPoint.x)+sqr(this->m_iPoint.y-_touch.m_iPoint.y))<=this->m_fRadius+_touch.m_fRadius)
			return true;
		else return false;
		break;
	case T_LINE:
		//printf("Calc!(%d,%d,%.2f,%d)\n",this->m_iPoint.y,_touch.m_sStart.y,m_fRadius,(this->m_iPoint.x-_touch.m_sStart.x)*(this->m_iPoint.x-_touch.m_sEnd.x));
		if(FastMath::Abs(this->m_iPoint.y-_touch.m_sStart.y)<=this->m_fRadius)
				if((this->m_iPoint.x-_touch.m_sStart.x)*(this->m_iPoint.x-_touch.m_sEnd.x)<=0)
				return true;
		return false;
		break;
	}

	return false;
}

void Character::SetPosition(int _x,int _y)
{
	global_pos_.x=_x;
	global_pos_.y=_y;
}